Home | | Latest | About | Random
# Header and implementation practice.
(in progress)
Write a header file called `my_functions.hpp` and its implementation `my_functions.cpp` that has the following functions that deals with `vector` of `int`s:
- `sum` that returns the sum of a `vector`
- `print_sum` that prints the sum of a `vector`
- `average` that returns the average of a `vector`
- `join` that returns a new `vector` that joins together two `vector`s
- `append` that modifies a `vector` by joining to it the elements of a second `vector`
- `sequence` that gives a `vector` that starts with an `int a`, and ends at `int b`, with step size `int t`. You will have to decide the implementation details.
- `print` that prints out the elements of a `vector` separated by two spaces ` `, and ends on the next line.
Make sure to choose the parameter types for your functions well, as well as making sure you obey `const` correctness. Watch out for when to use `using namespace`, and when to `#include` what.
A suggested answer for this suite of functions:
The header file `my_functions.hpp`
```cpp
#include
#include
/**
Finds the sum of a vector of ints
@param data : the vector of ints
@return the sum
*/
int sum(const std::vector& data);
/**
Prints the sum of a vector of ints
@param data : the vector of ints
*/
void print_sum(const std::vector& data);
/**
Finds the average of a vector of ints
@param data : the vector of ints
@return the average
*/
double average(const std::vector& data);
/**
Creates a new vector of ints by joining two vector of ints
@param a : the first vector of ints
@param b : the second vector of ints
@return a new vector of ints with the elements of a followed by b.
*/
std::vector join(const std::vector& a, const std::vector& b);
/**
Modifies a vector of ints by adding to it
the elements of another vector of ints
@param a : the first vector of ints, to be modified
@param b : the second vector of ints that provides the elements
*/
void append(std::vector& a, const std::vector& b);
/**
Gives a vector of ints that starts counting from int a, up to
possibly including b, with step size t
@param a : the starting value
@param b : the ending value (possibly including it)
@return if t <= 0, return empty vector {};
if a == b, return {a};
if a < b, return {a, a+t, a+2t, ...}
so that the last element does not exceed b
if a > b, return {a, a-t, a-2t, ...}
so that the last element does not get below b
any other case return empty vector {}
*/
std::vector sequence(int a, int b, int t);
/**
Prints out the entries of a vector of ints,
separated by two spaces, and ends on a newline.
@param v : the vector of ints
*/
void print(const std::vector& v);
```
The implementation `my_functions.cpp`
```cpp
#include "my_functions.hpp"
using namespace std;
int sum(const vector& data) {
int total = 0;
for (size_t i = 0 ; i < data.size(); i++){
total += data.at(i);
}
return total;
}
void print_sum(const vector& data) {
cout << sum(data) << endl;
}
double average(const vector& data) {
double s = sum(data); // implicit casts sum from int to double
return s / data.size(); // can also use static_cast
}
vector join(const vector& a, const vector& b) {
vector result;
for (size_t i = 0; i < a.size(); ++i) {
result.push_back(a.at(i));
}
for (size_t i = 0; i < b.size(); ++i) {
result.push_back(b.at(i));
}
return result;
}
void append(vector& a, const vector& b) {
size_t N = b.size(); // can you see why it's good to set N here?
for (size_t i = 0; i < N; ++i) {
a.push_back(b.at(i));
}
}
vector sequence(int a, int b, int t) {
vector result;
if (t <= 0) {return result;}
if (a == b) {
result.push_back(a);
return result;
}
if (a < b){
int current = a;
while (current <= b) {
result.push_back(current);
current += t;
}
return result;
}
if (a > b){
int current = a;
while (current >= b) {
result.push_back(current);
current -= t;
}
return result;
}
return result;
}
void print(const vector& v) {
for(size_t i = 0; i < v.size(); ++i) {
cout << v.at(i) << " ";
}
cout << endl;
}
```
To use it, we can write a `main.cpp` and have the following to play around with the functions we made:
```cpp
#include "my_functions.hpp"
using namespace std;
int main() {
vector a(10, 3);
vector b = sequence(5, -5, 3);
vector c = join(a,b);
print(c);
append(c, a);
print(c);
print_sum(c);
cout << c.size() << endl;
cout << average(c);
return 0;
}
```